class Snake {
  constructor() {
    this.body = [{x: 90, y: 90}];
    this.direction = 'right';
    this.speed = 300;
  }

  move() {
    const head = {x: this.body[0].x, y: this.body[0].y};
    const gridSize = 30;
    
    switch(this.direction) {
      case 'up':
        head.y -= gridSize;
        break;
      case 'down':
        head.y += gridSize;
        break;
      case 'left':
        head.x -= gridSize;
        break;
      case 'right':
        head.x += gridSize;
        break;
    }
    
    this.body.unshift(head);
    this.body.pop();
  }

  draw(ctx) {
    this.body.forEach((segment, index) => {
      // 绘制蛇身体
      if (index > 0) {
        ctx.fillStyle = '#7CB342';
        ctx.beginPath();
        ctx.arc(segment.x + 15, segment.y + 15, 12, 0, Math.PI * 2);
        ctx.fill();
        
        // 添加身体细节
        ctx.fillStyle = '#689F38';
        ctx.beginPath();
        ctx.arc(segment.x + 15, segment.y + 15, 8, 0, Math.PI * 2);
        ctx.fill();
      }
      
      // 绘制蛇头
      if (index === 0) {
        // 头部使用更深的颜色和更大的尺寸
        ctx.fillStyle = '#1B5E20';
        ctx.beginPath();
        ctx.arc(segment.x + 15, segment.y + 15, 18, 0, Math.PI * 2);
        ctx.fill();
        
        // 添加更醒目的眼睛
        ctx.fillStyle = 'white';
        ctx.beginPath();
        ctx.arc(segment.x + 8, segment.y + 8, 4, 0, Math.PI * 2);
        ctx.arc(segment.x + 22, segment.y + 8, 4, 0, Math.PI * 2);
        ctx.fill();
        ctx.fillStyle = '#0D47A1';
        ctx.beginPath();
        ctx.arc(segment.x + 8, segment.y + 8, 3, 0, Math.PI * 2);
        ctx.arc(segment.x + 22, segment.y + 8, 3, 0, Math.PI * 2);
        ctx.fill();
        
        // 添加嘴巴
        ctx.strokeStyle = 'white';
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.arc(segment.x + 15, segment.y + 20, 5, 0, Math.PI);
        ctx.stroke();
      }
    });
  }

  checkCollisionWithFood(food) {
    const head = this.body[0];
    const distance = Math.sqrt(Math.pow(head.x - food.x, 2) + Math.pow(head.y - food.y, 2));
    return distance < 15; // 当蛇头与食物距离小于15像素时判定为吃到
  }

  checkCollisionWithWall() {
    const head = this.body[0];
    return head.x < 0 || head.x > 800 || head.y < 0 || head.y > 600;
  }

  checkCollisionWithSelf() {
    const head = this.body[0];
    return this.body.slice(1).some(segment => segment.x === head.x && segment.y === head.y);
  }
  
  grow() {
    // 在蛇尾添加一个新的身体段
    const tail = this.body[this.body.length - 1];
    this.body.push({x: tail.x, y: tail.y});
  }
}

export { Snake };